home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / brandimg < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  19.3 KB  |  567 lines

  1. #!/bin/ksh
  2. # @(#) brandimg.ksh 1.2 96/11/27
  3. # 94/04/17 john h. dubois iii (john@armory.com)
  4. # 94/12/13 ksh compatibility fix
  5. # 95/01/03 More fixes for pd-ksh compatibility
  6. # 96/01/19 Added T option.
  7. # 96/11/27 Allow other input formats than pnm.  Added GE extension.
  8.  
  9. # Replacement for print, because pd-ksh(?) doesn't have -u option.
  10. alias print=uprint
  11. function uprint {
  12.     if [ "$1" = -u2 ]; then
  13.     shift    # get rid of -u2
  14.     \print "$@" >&2
  15.     else
  16.     \print "$@"
  17.     fi
  18. }
  19.  
  20. # Usage: pnminfo File
  21. # Puts the width of File in InfX, and the height in InfY
  22. # Changed to use command line substitution because pd-ksh creates a
  23. # separate shell for pipes, so pipe-into-read doesn't work the way we want.
  24. typeset -i InfX InfY
  25. function pnminfo {
  26. #    typeset j1 j2 j3 j4
  27.  
  28.     if [ $# -gt 0 ]; then
  29. #    pnmfile "$1" | read j1 type j2 InfX j3 InfY j4
  30.     set -- $(pnmfile "$1")
  31.     InfX=$4 InfY=$6
  32.     else
  33. #    pnmfile | read j1 type j2 InfX j3 InfY
  34.     set -- $(pnmfile)
  35.     InfX=$4 InfY=$6
  36.     fi
  37. }
  38.  
  39. # Finds the width and height of the piece to cut out of the image for branding,
  40. # and stores them in globals Width and Height.
  41. # Also sets ExtractProc and InsertProc to pipelines that should be used in
  42. # extracting material from image and inserting modified material back into
  43. # image, if it is neccessary to flip the material or truncate the brand.
  44. # Usage: FindCutParms filename brand-width brand-height
  45. # filename is the image file to cut from.
  46. # brand-width and brand-height are the dimensions of the branding files.
  47. # Other global vars used: Debug
  48. typeset -i Width Height
  49. function FindCutParms {
  50.     typeset File=$1 Flip=false
  51.     typeset -i BrandWidth=$2 BrandHeight=$3 tm
  52.  
  53.     ExtractProc=
  54.     InsertProc=
  55.     Width=BrandWidth
  56.     Height=BrandHeight
  57.     pnminfo $File
  58.     $Debug && print -u2 "Image $File: width=$InfX height=$InfY"
  59.     # -le is used instead of -lt because image must be one pixel larger
  60.     # than brand due to bug in some pnm utilities
  61.     if [ InfX -le BrandWidth ]; then
  62.     if [ InfY -gt BrandWidth ]; then
  63.         Flip=true
  64.     else
  65.         $Debug && print -u2 "Brand won't fit; truncating."
  66.         let Width=Width-1
  67.         ExtractProc="|Expand 0 $((BrandWidth-Width)) 0 0"
  68.         InsertProc="pnmcut 0 0 $Width $Height|"
  69.         print -u2 \
  70. "Truncated brand for $File to $(((100*Width)/BrandWidth))% of original length."
  71.         [ InfX -lt InfY ] && Flip=true
  72.     fi
  73.     fi
  74.     if $Flip; then
  75.     tm=Width
  76.     Width=Height
  77.     Height=tm
  78.     ExtractProc="|pnmflip -ccw $ExtractProc"
  79.     InsertProc="$InsertProc pnmflip -cw|"
  80.     $Debug && print -u2 "Inserting brand vertically."
  81.     else
  82.     $Debug && print -u2 "Inserting brand horizontally."
  83.     fi
  84.     $Debug && print -u2 \
  85.     "Addtl extraction pipe: $ExtractProc\nAddtl insertion pipe: $InsertProc"
  86.     return 0
  87. }
  88.  
  89. # Usage: Invert conversion-type source-material mask
  90. # Operations are performed on the source material to make it suitable for
  91. # branding.
  92. # Material is then cut out using mask and sent to the standard output.
  93. # Conversion is one of [iIbBmM]
  94. # iI Invert only    bB Quantize to 8 colors        mM Render in black/white
  95. # IBM Convolve first
  96. # Source is inverted, then convolved if required, 
  97. # then quantized or reduced to black/white if required.
  98. # Global vars used: ConvolTmp, QuantTmp
  99. function Invert {
  100.     typeset Conversion=$1 Source=$2 Mask=$3 Cmd=
  101.  
  102.     if [[ "$Conversion" = [IBM] ]]; then
  103.     # This convolution replaces each pixel with the average of its
  104.     # neighbors, with no weight given to the pixel itself.
  105.     # Note that the edges pixels won't be convolved since it's only
  106.     # done on pixels that have neighbors all around.
  107.     Cmd="|pnmconvol $ConvolTmp"
  108.     [ ! -f $ConvolTmp ] && echo \
  109. "P2
  110. 3 3
  111. 16
  112. 9 9 9
  113. 9 8 9
  114. 9 9 9" > $ConvolTmp
  115.     fi
  116.  
  117.     case "$Conversion" in
  118.     [bB]) Cmd="$Cmd | ppmquant -map $QuantTmp"
  119.     # This will push colors to 0 or full value
  120.     [ ! -f $QuantTmp ] && echo \
  121. "P3
  122. 8 1
  123. 255
  124.   0   0   0
  125. 255   0   0
  126.   0 255   0
  127.   0   0 255
  128. 255 255   0
  129. 255   0 255
  130.   0 255 255
  131. 255 255 255" > $QuantTmp
  132.     ;;
  133.     [mM])
  134.     Cmd="$Cmd | ppmtopgm | pgmtopbm -threshold -value 0.5"
  135.     ;;
  136.     esac
  137.     # Convert source to the form that we want to use,
  138.     # and then cut what we want out of it
  139.     $Debug && print -u2 \
  140.     "Inversion type: $Conversion\nInversion pipe extra commands: $Cmd
  141. Inversion source file: $Source\nInversion mask: $Mask"
  142.     eval pnminvert $Source $Cmd | pnmarith -mult $Mask -
  143.     return 0
  144. }
  145.  
  146. # Usage: MakeText "text" output-file border-width [fontfile]
  147. # Makes pbm text, using fontfile if given, and puts output in output-file.
  148. # The text is cropped to remove margins.
  149. # If border-width is nonzero, a margin of the given thickness is left.
  150. # Sets globals TextWidth and TextHeight to width and height of output file.
  151. # Other global variables: CroppedTmp, Debug
  152. function MakeText {
  153.     typeset Text=$1 OutputFile=$2 FontOpt FontFile TmpOutput
  154.     typeset -i BorderWidth=$3
  155.  
  156.     if [ $# -eq 4 ]; then
  157.     FontFile=$4
  158.     $Debug && print -u2 "Font file is $FontFile"
  159.     if [ ! -r "$FontFile" ]; then
  160.         print -u2 "Cannot read font file $FontFile.  Exiting."
  161.         exit 1
  162.     fi
  163.     FontOpt="-font $FontFile"
  164.     fi
  165.     [ $BorderWidth -eq 0 ] && TmpOutput=$OutputFile || TmpOutput=$CroppedTmp
  166.     # Fonts are black on white.
  167.     # Crop white off, then invert to give white on black.
  168.     pbmtext $FontOpt "$Text" | pnmcrop -white | pnminvert > $TmpOutput
  169.     pnminfo $TmpOutput
  170.     if [ $BorderWidth -ne 0 ]; then
  171.     # Add margin around text.
  172.     TextWidth="InfX+(2*BorderWidth)"
  173.     TextHeight="InfY+(2*BorderWidth)"
  174.     pbmmake -black $TextWidth $TextHeight |
  175.     pnmpaste -replace $TmpOutput $BorderWidth $BorderWidth > $OutputFile
  176.     else
  177.     TextWidth=InfX
  178.     TextHeight=InfY
  179.     fi
  180.     $Debug && print -u2 "Text width=$TextWidth; height=$TextHeight"
  181.     return 0 
  182. }
  183.  
  184. # Usage: 
  185. # MakeMasks text width height border-width text-color allmask-file bmask-file
  186. # Make a white-on-black masks for the border and for all of the fixed material
  187. # (the text mask is the text itself).  
  188. # Textfile should be white on black.  Its size is given by width & height.
  189. # Border mask is written to bmask-file.
  190. # All-fixed-mask is written to allmask-file.
  191. # Text is grown by border-width (1 or more) pixels.  The grown part is the
  192. # border mask; the text and grown part is the all-mask.
  193. # If text-color is n (indicating that only the border will be masked into the
  194. # image), the grown part only is used for the all-mask.
  195. # Global vars used: FullMaskInTmp, FullMaskOutTmp
  196. function MakeMasks {
  197.     typeset GrowConv tm TextFile=$1 Cmd= FullTee= BOnlyTee=
  198.     typeset -i TextWidth=$2 TextHeight=$3 BorderWidth=$4
  199.     typeset TextColor=$5 AllMaskFile=$6 BorderMaskFile=$7
  200.  
  201.     GrowConv="P2
  202. 3 3
  203. 2
  204. 2 2 2
  205. 2 2 2
  206. 2 2 2"
  207.     # First, make a mask that is one pixel larger, because pnmconvol won't
  208.     # operate on the outermost pixels given a 3x3 convolution matrix
  209.     pbmmake -black $((TextWidth+2)) $((TextHeight+2)) |
  210.     pnmpaste -replace $TextFile 1 1 > $FullMaskInTmp
  211.     # Run as many iterations as neccessary.
  212.     # Use pnmconv & pgmtopbm instead of using pbmmask because pbmmask will
  213.     # decide the text is the background if it gets too fat.  Also, pbmmask
  214.     # fills in enclosed areas, which we don't want.
  215.     while [ BorderWidth -gt 1 ]; do
  216.     echo "$GrowConv" | pnmconvol - $FullMaskInTmp | 
  217.     pgmtopbm -threshold -value 0.1 > $FullMaskOutTmp
  218.     # Swap names of input & output
  219.     tm=$FullMaskOutTmp
  220.     FullMaskOutTmp=$FullMaskInTmp
  221.     FullMaskInTmp=$tm
  222.     let BorderWidth-=1
  223.     done
  224.  
  225.     # Insert the tee that writes the all-mask-file either before or after
  226.     # the text is XORed out.
  227.     [ "$TextColor" = n ] && BOnlyTee="| tee $AllMaskFile" ||
  228.     FullTee="| tee $AllMaskFile"
  229.  
  230.     $Debug && print -u2 "No-text-cmd: $Cmd"
  231.     # Do last grow step.
  232.     # Cut off the temporary margin that was added.
  233.     # If image will be left where original text is, subtract it from mask.
  234.     echo "$GrowConv" | pnmconvol - $FullMaskInTmp | 
  235.     pgmtopbm -threshold -value 0.1 | 
  236.     eval pnmcut 1 1 $TextWidth $TextHeight - $FullTee |
  237.     eval pnmpaste -xor $TextFile 0 0 - $BOnlyTee > $BorderMaskFile
  238. }
  239.  
  240. # Make fixed-color text and/or border & write it to stdout
  241. # Usage: MakeFixed text-color border-color text-mask border-mask
  242. # text-mask and border-mask are white-on-black masks to colorize.
  243. # Globals used: BorderTmp, Debug
  244. function MakeFixed {
  245.     typeset TextColor=$1 BorderColor=$2 TextMask=$3 BorderMask=$4
  246.     typeset RealBorder RealText BorderCmd
  247.  
  248.     # Make text/border black if they won't included or if their color will
  249.     # be derived from image material
  250.     [[ "$BorderColor" = [niIbBmM] ]] && RealBorder=black ||
  251.     RealBorder=$BorderColor
  252.     [[ "$TextColor" = [niIbBmM] ]] && RealText=black || RealText=$TextColor
  253.     $Debug && print -u2 "Real colors: border=$RealBorder, text=$RealText"
  254.  
  255.     if [ $RealBorder = black ]; then
  256.     # BorderMask will not exist if no border
  257.     pgmtoppm $RealText $TextMask
  258.     else
  259.         pgmtoppm $RealBorder $BorderMask > $BorderTmp
  260.     $Debug && print -u2 "Adding border and text."
  261.     pgmtoppm $RealText $TextMask | pnmarith -add $BorderTmp -
  262.     fi
  263.     return 0
  264. }
  265.  
  266. # Usage: GetUnCom filename.ext
  267. # Sets global getUnCom_ret to the name of an uncompressor appropriate for the
  268. # given file, based on its extension.  If no appropriate uncompressor is known,
  269. # getUnCom_ret is set to a null string.
  270. function getUnCom {
  271.     case "${1##*.}" in
  272.     [jJ][pP]?([eE])[gG])
  273.     getUnCom_ret=djpeg
  274.     getUnCom_un=cjpeg
  275.     ;;
  276.     [gG][iI][fF])
  277.     getUnCom_ret=giftopnm
  278.     getUnCom_un=ppmtogif
  279.     ;;
  280.     [pP][bBgGpPnN][mM])
  281.     getUnCom_ret=cat
  282.     getUnCom_un=cat
  283.     ;;
  284.     *)
  285.     getUnCom_ret=
  286.     getUnCom_un=
  287.     ;;
  288.     esac
  289. }
  290.  
  291. # Usage: topnm filename [command [args]]
  292. # A converter to pnm format is chosen based on the filename extension.
  293. # If a command is given, the output of the converter is piped into it;
  294. # otherwise it is sent to the standard output.
  295. # As an optimization, if the file is already in pnm format and a command is
  296. # given, it is passed as the final argument to command.  Therefore, a command
  297. # should only be given if it is appropriate to pass it a pnm file after all
  298. # of its fixed arguments.
  299. # The global DEF_EXT may provide a default for files with unknown extensions.
  300. # The global name should be set to the program name.
  301. function topnm {
  302.     typeset uncom= ext filename=$1
  303.     shift
  304.  
  305.     getUnCom "$filename"
  306.     [ -z "$getUnCom_ret" -a -n "$DEF_EXT" ] && getUnCom "$DEF_EXT"
  307.     if [ -z "$getUnCom_ret" ]; then
  308.     print -u2 -- "$name: Do not know how to convert file: $filename"
  309.     return 1
  310.     fi
  311.     if [ $# -gt 0 ]; then
  312.     [ "$getUnCom_ret" = cat ] && "$@" "$filename" ||
  313.     "$getUnCom_ret" < "$filename" | "$@"
  314.     else
  315.     "$getUnCom_ret" < "$filename"
  316.     fi
  317.     return $?
  318. }
  319.  
  320. name=${0##*/}
  321. Usage=\
  322. "Usage: $name [-hoPx] [-b<text-border-type>] [-t<text-type>] [-e<extension>]
  323.        [-w<border-width>] [-f<pbm-font-file>] [-T<tmp-prefix>] \"text\"
  324.        image-file ..."
  325.  
  326. BorderColor=blue TextColor=white Extension=.B
  327. Debug=false Preserve=false Stdout=false
  328. typeset -i BorderWidth=1
  329. Tmp=
  330. progress=false
  331. DEF_EXT=
  332.  
  333. while getopts :e:f:hb:t:w:oxPT:E:G opt; do
  334.     case $opt in
  335.     h)
  336.     echo \
  337. "$name: imprint text on an image.
  338. $name masks the given text into the bottom of an image, right adjusted and
  339. surrounded by a one-pixel-wide border to ensure that the text can be seen
  340. regardless of the color of the area it is inserted into.  The border is not a
  341. box; it flows around the text itself.
  342. If the text is too long to fit, it is put along the right of the image, bottom
  343. adjusted.  If it is still too long to fit, it is put along the longer dimension
  344. with the right of the text truncated and a warning is printed.
  345. Output is written to a file with name of the source file with .B appended, and
  346. is in the same format as the input file.  The file type is derived from the
  347. filename extension.  $name knows how to deal with jpeg, gif, and pbm/pgm/ppm
  348. files.
  349. $Usage
  350. Options:
  351. -h: Print this help.
  352. -b, -t: These options specify alternate material to use for the border and text
  353.     respectively.  The default is $TextColor text with a $BorderColor border.
  354.     A value of 'n' turns off that component.  -bn removes the border; -tn makes
  355.     the image show through inside the border.  If either is given and the
  356.     material inserted is the same color as the insertion area, it won't show
  357.     up.  A value of 'i' makes that component consist of the image material
  358.     with red, green, and blue each inverted separately.  The closer the 
  359.     insertion area is to 50% intensity gray, the less the text will show up.
  360.     A value of 'b' is like i, except that the results of the inversion are
  361.     thresholded such that each color has either 0 or full intensity.
  362.     A value of 'm' is like b, except that only black and white are used.
  363.     I, B, and M are the like i, b, and m except that the image material that
  364.     will be used for the text is first run through a convolution such that each
  365.     pixel is replaced with an average of its neighbors, with 0 weight given to
  366.     the pixel itself, so that the color of the text will be derived from the
  367.     pixels that will be next to it rather than the pixels that are replaced.
  368.     If any other value is given with -b or -t, it is taken to be a color to set
  369.     that part to.  See the pgmtoppm man page for a description of how colors
  370.     may be specified.
  371. -o: Write output to standard output.  Only one filename should be given.
  372. -x: Turn on debugging.
  373. -P: Preserve (do not remove) tmp files.
  374. -G: Print a progress indication to the standard error output.  As each file is
  375.     about to be processed, its name is printed.
  376. -T: Set the tmp file prefix.  By default, tempfiles are placed in the invoking
  377.     user's home directory (or the directory specified by the TMP environment
  378.     variable, if set), and begin with '#b'.  If a prefix is given with -T,
  379.     tempfile names are prefixed with the prefix.  If the prefix does not
  380.     include a directory component, tempfiles are placed in the current working
  381.     directory.
  382. -e: Write output to files with the given extension appended, instead of \".B\".
  383. -E: The given extension specifies a default type for files that have no
  384.     extension or whose extension is not recognized.  The extension given should
  385.     be one of gif, jpg, pbm, pgm, or ppm.
  386. -w: Set the border width (thickness).  The default is 1 pixel.
  387. -f: Use a font other than the default.  The font file should be created by
  388.     the means described in the pbmtext man page."
  389.     exit 0
  390.     ;;
  391.     b)  BorderColor=$OPTARG
  392.     [ "$BorderColor" = n ] && BorderWidth=0
  393.     ;;
  394.     t)  TextColor=$OPTARG
  395.     ;;
  396.     T)  Tmp=$OPTARG;;
  397.     o)    Stdout=true; unset OutFile;;
  398.     x)  Debug=true;;
  399.     G)  progress=true;;
  400.     P)  Preserve=true;;
  401.     e)    Extension=$OPTARG;;
  402.     E)    DEF_EXT=$OPTARG
  403.     getUnCom "$DEF_EXT"
  404.     if [ -z "$getUnCom_ret" ]; then
  405.         print -u2 "$name: Unrecognized extension given with -E: $DEF_EXT"
  406.         exit 1
  407.     fi
  408.     ;;
  409.     w)    BorderWidth=$OPTARG;;
  410.     f)  FontFile=$OPTARG;;
  411.     +?)
  412.     print -u2 "$name: options should not be preceded by a '+'."
  413.     exit 1
  414.     ;;
  415.     :) 
  416.     print -u2 "$name: Option '$OPTARG' requires a value.  Use -h for help."
  417.     exit 1
  418.     ;;
  419.     ?) 
  420.     print -u2 "$name: $OPTARG: bad option.  Use -h for help."
  421.     exit 1
  422.     ;;
  423.     esac
  424. done
  425.  
  426. if [ "$BorderColor" = n -a "$TextColor" = n ]; then
  427.     print -u2 "No border or text to be inserted.  Aborting."
  428.     exit 1
  429. fi
  430.  
  431. # remove args that were options
  432. let OPTIND=OPTIND-1
  433. shift $OPTIND
  434.  
  435. # Need at least text and one image file
  436. if [ $# -lt 2 ]; then
  437.     print -u2 "$Usage\nUse -h for help."
  438.     exit
  439. fi
  440.  
  441. # Quit if anything goes wrong
  442. set -e
  443.  
  444. Text=$1
  445. shift
  446.  
  447. : ${Tmp:=${TMP:-$HOME}/#b}
  448. BorderTmp=$Tmp.fixbdr.$$    # Fixed border material
  449. ImageBdrTmp=$Tmp.imgbdr.$$    # Image-derived border material
  450. BorderMaskTmp=$Tmp.brdmsk.$$    # Mask for border material
  451. ConvolTmp=$Tmp.imgcnv.$$    # Image convolution matrix
  452. CroppedTmp=$Tmp.rawtxt.$$    # pbm text, after cropping & before expansion
  453. TextMaskTmp=$Tmp.exptxt.$$    # White-on-black expanded text
  454. FullMaskInTmp=$Tmp.bdr-in.$$    # Used in generating border
  455. FullMaskOutTmp=$Tmp.bdrout.$$    # Used in generating border
  456. ImagePieceTmp=$Tmp.img-pc.$$    # Text-size piece cut from image
  457. ImgMaskTmp=$Tmp.imgmsk.$$    # Mask used to cut space in image for brand
  458. AllBrandTmp=$Tmp.albrnd.$$    # Fixed and image-derived brand to add to image
  459. FixedMtlTmp=$Tmp.fxbrnd.$$    # Fixed branding material (text and/or border)
  460. QuantTmp=$Tmp.quantz.$$        # Quantization colors
  461. ConvertTmp=$Tmp.conv.$$        # Image converted to pnm
  462.  
  463. $Debug && print -u2 "tmpfile prefix is $Tmp"
  464.  
  465. $Preserve ||
  466. trap "rm -f $FixedMtlTmp $ImgMaskTmp $BorderTmp $TextMaskTmp $CroppedTmp "\
  467. "$FullMaskInTmp $FullMaskOutTmp $ImagePieceTmp $AllBrandTmp $QuantTmp "\
  468. "$ConvolTmp $BorderMaskTmp $ImageBdrTmp $ConvertTmp" EXIT 1 2 3 15
  469.  
  470. typeset -i TextWidth TextHeight
  471.  
  472. # Make raw text & put in $TextMaskTmp
  473. MakeText "$Text" $TextMaskTmp $BorderWidth $FontFile
  474.  
  475. # BorderMaskTmp is set up only if there is a border
  476. if [ $BorderWidth -gt 0 ]; then
  477.     # Make mask for fixed material & put it in $ImgMaskTmp
  478.     MakeMasks $TextMaskTmp $TextWidth $TextHeight $BorderWidth $TextColor \
  479.     $ImgMaskTmp $BorderMaskTmp
  480. else
  481.     ImgMaskTmp=$TextMaskTmp
  482. fi
  483.  
  484. # Put the fixed-color part of the material to be masked in in $FixedMtlTmp.
  485. MakeFixed $TextColor $BorderColor $TextMaskTmp $BorderMaskTmp > $FixedMtlTmp
  486.  
  487. # FixedMtlTmp is the fixed-color material.
  488. # ImgMaskTmp is the mask, white where image should be removed.
  489. # TextMaskTmp is the white-on-black text bitmap.
  490. # BorderTmp & CroppedTmp vars are no longer used but may refer to the same
  491. # files as the other vars.
  492.  
  493. # Put 'do' on separate line, for pd-ksh
  494. for infile
  495. do
  496.     $progress && print -u2 -- "$infile"
  497.     if [ ! -f "$infile" -o ! -r "$infile" ]; then
  498.     print -u2 -- "$name: Cannot read file: $infile"
  499.     exit 1
  500.     fi
  501.     getUnCom "$infile"
  502.     case "$getUnCom_ret" in
  503.     cat)
  504.     file=$infile
  505.     unConvert=
  506.     ;;
  507.     "")
  508.     print -u2 -- "$name: Do not know how to convert file: $infile"
  509.     continue
  510.     ;;
  511.     *)
  512.     topnm "$infile" > $ConvertTmp || {
  513.         print -u2 -- \
  514.         "$name: Conversion to pnm format failed for file: $infile"
  515.         continue
  516.     }
  517.     file=$ConvertTmp
  518.     unConvert="| $getUnCom_un"
  519.     ;;
  520.     esac
  521.     # Set Width & Height to dimensions of piece to cut out, etc.
  522.     FindCutParms $file $TextWidth $TextHeight
  523.  
  524.     # Do not rely in existance of /dev/stdout et al
  525.     $Stdout || {
  526.     if [ "$infile" -ef $infile$Extension ]; then
  527.         print -u2 "Input and output are the same file."
  528.         continue
  529.     fi
  530.     OutFile="> '$infile$Extension'"
  531.     }
  532.     $Debug && print -u2 "Stdout=$Stdout.  Output is $OutFile"
  533.  
  534.     # Get image piece
  535.     eval pnmcut -$Width -$Height $Width $Height '"$file"' $ExtractProc \
  536.     > $ImagePieceTmp
  537.  
  538.     # Make image-derived brand components, if neccessary
  539.     if [[ "$TextColor" = [iIbBmM] || "$BorderColor" = [iIbBmM] ]]; then
  540.     # Don't bother adding fixed color material if it's just black
  541.     if [[ "$TextColor" != [niIbBmM] || "$BorderColor" != [niIbBmM] ]]; then
  542.         FixedCmd="| pnmarith -add - $FixedMtlTmp" # Add fixed part of brand
  543.         $Debug && print -u2 "Fixed material add command: $FixedCmd"
  544.     else
  545.         $Debug && print -u2 "No fixed material to add."
  546.     fi
  547.     TextInvCmd="Invert $TextColor $ImagePieceTmp $TextMaskTmp"
  548.     BorderInvCmd="Invert $BorderColor $ImagePieceTmp $BorderMaskTmp"
  549.     if [[ "$TextColor" != [iIbBmM] ]]; then    # Only border is image derived
  550.         eval $BorderInvCmd $FixedCmd
  551.     elif [[ "$BorderColor" != [iIbBmM] ]]; then  # Only text image derived
  552.         eval $TextInvCmd $FixedCmd
  553.     else    # Both are image derived
  554.         eval $BorderInvCmd > $ImageBdrTmp
  555.         eval $TextInvCmd $FixedCmd | pnmarith -add - $ImageBdrTmp
  556.     fi > $AllBrandTmp
  557.     else
  558.     AllBrandTmp=$FixedMtlTmp
  559.     fi
  560.  
  561.     # Put it all back together
  562.     pnmarith -sub $ImagePieceTmp $ImgMaskTmp | # Cut mask out of image
  563.     pnmarith -add - $AllBrandTmp | # Add brand
  564.     eval $InsertProc pnmpaste -replace - -$Width -$Height '"$file"' \
  565.     $unConvert $OutFile
  566. done
  567.